package ru.spb.osll.myfit.client.dialog;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ErrorDialog extends DialogBox {
  /**
   * The message displayed to the user when the server cannot be reached or
   * returns an error.
   */
  private static final String SERVER_ERROR = "An error occurred while "
      + "attempting to contact the server. Please check your network " + 
      "connection and try again.";
  private Label m_serverResponseLabel;
  
  public ErrorDialog() {
    setText("An error occured");
    setAnimationEnabled(true);
    final Button closeButton = new Button("Ok");
    closeButton.getElement().setId("closeButton");
    m_serverResponseLabel = new Label();
    VerticalPanel dialogVPanel = new VerticalPanel();
    dialogVPanel.addStyleName("dialogVPanel");
    dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
    dialogVPanel.add(m_serverResponseLabel);
    setWidget(dialogVPanel);
//    setText("Remote Procedure Call - Failure");
    m_serverResponseLabel.addStyleName("serverResponseLabelError");
    m_serverResponseLabel.setText(SERVER_ERROR);
    dialogVPanel.add(closeButton);

    closeButton.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        ErrorDialog.this.hide();
      }
    });
  }
  
  public void showWithMessage(String message) {
    setMessage(message);
    super.show();
  }
  
  public void setMessage(String message){
    m_serverResponseLabel.setText(message);
  }
  
  public void setMessage(Throwable e){
    String message = e.getClass().toString() + " : " + e.getMessage() + "\n";
    for(StackTraceElement elem: e.getStackTrace()) {
      message = message + elem.toString() + "\n";
    }
    setMessage(message);
  }
  
  

}
